/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.externalreference;

import java.util.HashSet;

import org.openaecfm.model.datetime.Date;
import org.openaecfm.model.datetime.DateTime;
import org.openaecfm.model.externalreference.DocumentConfidentialityEnum;
import org.openaecfm.model.externalreference.DocumentInformation;
import org.openaecfm.model.externalreference.DocumentStatusEnum;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class DocumentInformationIO extends
		AbstractEntityInstanceInputImpl<DocumentInformation> implements
		IEntityInstanceOutput<DocumentInformation> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(DocumentInformation pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCDOCUMENTINFORMATION", appDataManager.getName(pojo));

		// Set Identification
		StringParameter docid = new StringParameter(pojo.getIdentification(),
				StringParameter.UTF_8);
		ei.addParameter(docid);

		// Set Name
		StringParameter name = new StringParameter(pojo.getName(),
				StringParameter.UTF_8);
		ei.addParameter(name);

		// Set Description
		if (pojo.getDescription() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getDescription(),
					StringParameter.UTF_8));
		}

		// Set Location
		if (pojo.getLocation() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getLocation(),
					StringParameter.UTF_8));
		}

		// Set Purpose
		if (pojo.getPurpose() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getPurpose(),
					StringParameter.UTF_8));
		}

		// Set IntendedUse
		if (pojo.getIntendedUse() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getIntendedUse(),
					StringParameter.UTF_8));
		}

		// Set Scope
		if (pojo.getScope() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getScope(),
					StringParameter.UTF_8));
		}

		// Set Revision
		if (pojo.getRevision() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getRevision(),
					StringParameter.UTF_8));
		}

		// Set DocumentOwner
		if (pojo.getDocumentOwner() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getDocumentOwner())));
		}

		// Set Editors
		if (pojo.getEditors() == null || pojo.getEditors().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Object e : pojo.getEditors()) {
				EntityInstanceNameParameter n = new EntityInstanceNameParameter(
						appDataManager.getName(e));
				l.add(n);
			}
			ei.addParameter(l);
		}

		// Set CreationTime
		if (pojo.getCreationTime() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getCreationTime()
					.toString(), StringParameter.UTF_8));
		}

		// Set LastRevisionTime
		if (pojo.getLastRevisionTime() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getLastRevisionTime()
					.toString(), StringParameter.UTF_8));
		}

		// Set ElectronicFormat
		if (pojo.getElectronicFormat() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getElectronicFormat(),
					StringParameter.UTF_8));
		}

		// Set ValidFrom
		if (pojo.getValidFrom() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getValidFrom().toString(),
					StringParameter.UTF_8));
		}

		// Set ValidUntil
		if (pojo.getValidUntil() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(
					pojo.getValidUntil().toString(), StringParameter.UTF_8));
		}

		// Set Confidentiality
		if (pojo.getConfidentiality() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getConfidentiality()));
		}

		// Set Status
		if (pojo.getStatus() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getStatus()));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected DocumentInformation doCreateObject() {
		return new DocumentInformation();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			DocumentInformation pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set DocumentOwner
		Parameter docowner = sei.getParameter(8);
		if (docowner.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) docowner;
			pojo.setDocumentOwner(appDataManager.getObject(n.toInstanceName()));
		}

		// Set Editors
		Parameter editors = sei.getParameter(9);
		if (editors.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) editors;
			if (pojo.getEditors() == null) {
				pojo.setEditors(new HashSet<Object>());
			}
			for (Parameter p : l) {
				EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
				pojo.getEditors().add(
						appDataManager.getObject(n.toInstanceName()));
			}
		}
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			DocumentInformation pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Identification
		StringParameter identification = (StringParameter) sei.getParameter(0);
		pojo.setIdentification(identification.toUTF8());

		// Set Name
		StringParameter name = (StringParameter) sei.getParameter(1);
		pojo.setName(name.toUTF8());

		// Set Description
		Parameter description = sei.getParameter(2);
		if (description.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) description;
			pojo.setDescription(s.toUTF8());
		}

		// Set Location
		Parameter location = sei.getParameter(3);
		if (location.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) location;
			pojo.setLocation(s.toUTF8());
		}

		// Set Purpose
		Parameter purpose = sei.getParameter(4);
		if (purpose.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) purpose;
			pojo.setPurpose(s.toUTF8());
		}

		// Set IntendedUse
		Parameter intendeduse = sei.getParameter(5);
		if (intendeduse.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) intendeduse;
			pojo.setIntendedUse(s.toUTF8());
		}

		// Set Scope
		Parameter scope = sei.getParameter(6);
		if (scope.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) scope;
			pojo.setScope(s.toUTF8());
		}

		// Set Revision
		Parameter revision = sei.getParameter(7);
		if (revision.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) revision;
			pojo.setRevision(s.toUTF8());
		}

		// Set CreationTime
		Parameter cretime = sei.getParameter(10);
		if (cretime.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) cretime;
			pojo.setCreationTime(new DateTime(s.toUTF8()));
		}

		// Set LastRevisionTime
		Parameter lasttime = sei.getParameter(11);
		if (lasttime.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) lasttime;
			pojo.setLastRevisionTime(new DateTime(s.toUTF8()));
		}

		// Set ElectronicFormat
		Parameter eformat = sei.getParameter(12);
		if (eformat.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) eformat;
			pojo.setElectronicFormat(s.toUTF8());
		}

		// Set ValidFrom
		Parameter vfrom = sei.getParameter(13);
		if (vfrom.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) vfrom;
			pojo.setValidFrom(new Date(s.toUTF8()));
		}

		// Set ValidUntil
		Parameter vuntil = sei.getParameter(14);
		if (vuntil.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) vuntil;
			pojo.setValidUntil(new Date(s.toUTF8()));
		}

		// Set Confidentiality
		Parameter confid = sei.getParameter(15);
		if (confid.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) confid;
			pojo.setConfidentiality(DocumentConfidentialityEnum.valueOf(e
					.toEnumString()));
		}

		// Set Status
		Parameter status = sei.getParameter(16);
		if (status.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) status;
			pojo.setStatus(DocumentStatusEnum.valueOf(e.toEnumString()));
		}
	}
}
